package com.xfly.app.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.utils.GlideUtil;
import com.xfly.app.bean.BankBean;
import com.xfly.app.bean.KcggBean;

import java.util.HashMap;
import java.util.List;


/**
 * Created by Administrator on 2018/1/12.
 */



public class KcggAdapter extends BaseAdapter {

    private Context mContext;
    private List<KcggBean> mDataList;
    private LayoutInflater mLayoutInflater;
    private int mTouchItemPosition = -1;
    private OnChangedTextListener onChangedTextListener;
    HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

    public KcggAdapter(Context context, List<KcggBean> list) {
        this.mContext = context;
        this.mDataList = list;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void pushData(List<KcggBean> list){
        this.mDataList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mDataList == null){
            return 0;
        }
        return mDataList.size();
    }

    Click click;

    public void setClick(Click click){
        this.click = click;
    }


    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_kc, parent,false);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.num = (EditText) convertView.findViewById(R.id.num);
            viewHolder.mTextWatcher = new MyTextChangeWatch();
            //设置数据监听
            viewHolder.num.addTextChangedListener(viewHolder.mTextWatcher);
            viewHolder.upDataPosition(position);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        KcggBean bean = mDataList.get(position);

        viewHolder.name.setText(bean.name);

        viewHolder.num.setText(mDataList.get(position).stock);
        viewHolder.num.setTag(position);
        viewHolder.num.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //使用getTag 记录position
                mTouchItemPosition = (int) v.getTag();
                return false;
            }
        });
        /**
         * 解决焦点问题
         * 当editView获取焦点时,listview会重新绘制,致使editView的焦点光标失去
         */
        if (mTouchItemPosition == position) {
            viewHolder.num.requestFocus();
            viewHolder.num.setSelection(viewHolder.num.getText().length());
        } else {
            viewHolder.num.clearFocus();
        }



        return convertView;
    }

    public List<KcggBean> getList(){
        return mDataList;
    }

    public HashMap<Integer, String> getHashMap() {
        return hashMap;
    }

    class ViewHolder {

        TextView name;
        EditText num;
        MyTextChangeWatch mTextWatcher;
        public void upDataPosition(int position) {
            mTextWatcher.upDataPosition(position);
        }
    }

    class MyTextChangeWatch implements TextWatcher {
        private int position;

        public void upDataPosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            hashMap.put(position, s.toString());
            if(onChangedTextListener !=null){
                //使用回调将数据传递出去,进行数据检测
                onChangedTextListener.onChangedTextListener(position, s.toString());
            }
        }
    }
    public void setOnChangedTextListener(OnChangedTextListener onChangedTextListener) {
        this.onChangedTextListener = onChangedTextListener;
    }
    interface OnChangedTextListener {
        void onChangedTextListener(int position, String str);
    }
    public  interface Click{
        void Click(int position,String num);
        void Click2(int position);
    }

}
